﻿@using Microsoft.AspNetCore.Http.Extensions
@using Microsoft.Extensions.Localization
@using Microsoft.Extensions.Options
@using Volo.Abp.Localization
@using Volo.Abp.Users
@using EventHub.Localization
@using EventHub.Web
@using System.Net
@using EventHub.Options
@inject ICurrentUser CurrentUser
@inject ILanguageProvider LanguageProvider
@inject IStringLocalizer<EventHubResource> L
@inject IOptions<EventHubUrlOptions> UrlOptions

@{
    var returnUrl = WebUtility.UrlEncode(Context.Request.GetEncodedPathAndQuery());
    if (returnUrl.IsNullOrWhiteSpace())
    {
        returnUrl = "/";
    }
}
<nav class="navbar navbar-expand-lg navbar-light bg-light static-top">
    <div class="container">
        <a class="navbar-brand font-weight-bold" href="@Url.Page("/index")">
            <img src="/assets/eventhub-logo.svg" class="logo"/>
        </a>
        <button class="navbar-toggler" type="button" data-bs-toggle="collapse" data-bs-target="#navbarResponsive" aria-controls="navbarResponsive" aria-expanded="false" aria-label="Toggle navigation">
            <span class="navbar-toggler-icon"></span>
        </button>
        <div class="collapse navbar-collapse" id="navbarResponsive">
            <ul class="navbar-nav mx-auto">
                <li class="nav-item @(GetPageActiveClassOrEmpty(ViewContext, "/index"))">
                    <a class="nav-link" href="@UrlOptions.Value.Www@Url.Page("/index")">
                        Home
                    </a>
                </li>
                <li class="nav-item @(GetPageActiveClassOrEmpty(ViewContext, "/events/index"))">
                    <a class="nav-link" href="@UrlOptions.Value.Www/events">Events</a>
                </li>
                <li class="nav-item @(GetPageActiveClassOrEmpty(ViewContext, "/organizations/index"))">
                    <a class="nav-link" href="@UrlOptions.Value.Www/organizations">Organizations</a>
                </li>
                <li class="nav-item @(GetPageActiveClassOrEmpty(ViewContext, "/about/index"))">
                    <a class="nav-link" href="@UrlOptions.Value.Www/about">About</a>
                    </li>
            </ul>
            @if (UrlOptions.Value.Www.Contains(Context.Request.Host.ToString()))
            {
                <ul class="navbar-nav">
                    <li class="nav-item">
                        <div class="dropdown">
                            <a class="nav-link event-link" type="button" id="createDropdownMenuButton" data-bs-toggle="dropdown" aria-haspopup="true" aria-expanded="false">
                                + Create
                            </a>
                            <div class="dropdown-menu m-2" aria-labelledby="dropdownMenuButton">
                                <a class="dropdown-item" href="@UrlOptions.Value.Www.EnsureEndsWith('/')organizations/new">Organization</a>
                                <div class="dropdown-divider"></div>
                                <a class="dropdown-item" href="@UrlOptions.Value.Www.EnsureEndsWith('/')events/new">Event</a>
                            </div>
                        </div>
                    </li>
                </ul>
            }
            <ul class="navbar-nav">

                @if (CurrentUser.IsAuthenticated)
                {
                    <li class="nav-item dropdown">
                        <a class="nav-link dropdown-toggle" href="#" id="navbarDropdownMenuLink" role="button" data-bs-toggle="dropdown" aria-haspopup="true" aria-expanded="false">
                            <span class="user-avatar">
                                <img gravatar-email="@CurrentUser.Email" default-image="Wavatar" gravatar-size="40" width="40" height="40" class="nav-avatar avatar rounded-circle"/>
                            </span>
                            <span class="ms-2">
                                @if (!string.IsNullOrWhiteSpace(CurrentUser.Name) && !string.IsNullOrWhiteSpace(CurrentUser.SurName))
                                {
                                    @(CurrentUser.Name + " " + CurrentUser.SurName)
                                }
                                else
                                {
                                    {
                                        @CurrentUser.UserName
                                    }
                                }
                            </span>
                        </a>
                        <div class="dropdown-menu dropdown-menu-right m-2">
                            <a class="dropdown-item" href="@(UrlOptions.Value.Account.EnsureEndsWith('/') + "account/manage")" title="@L["Account"]">
                                <i class="fas fa-cog"></i> @L["Account"]
                            </a>
                            <a class="dropdown-item" href="@UrlOptions.Value.Www/users/@CurrentUser.UserName" title="@L["Profile"]">
                                <i class="fas fa-user"></i> @L["Profile"]
                            </a>
                            <a class="dropdown-item" href="/account/logout?returnUrl=@returnUrl" title="@L["Logout"]">
                                <i class="fas fa-sign-out-alt"></i> @L["Logout"]
                            </a>
                        </div>
                    </li>
                }
                else
                {
                    <li class="nav-item">
                        <a class="nav-link" href="/account/login?returnUrl=@returnUrl">Log In</a>
                    </li>
                    <li class="nav-item">
                        <a class="btn btn-primary ms-lg-2" href="@UrlOptions.Value.Account/account/register?returnUrl=@returnUrl">Sign Up</a>
                    </li>
                }
            </ul>
        </div>
    </div>
</nav>

@functions{

    public string GetPageActiveClassOrEmpty(ViewContext viewContext, string pageUrl)
    {
        if (!UrlOptions.Value.Www.Contains(Context.Request.Host.ToString()))
        {
            return "";
        }

        var activeRoute = viewContext?.RouteData.Values["page"]?.ToString()?.ToLowerInvariant();
        pageUrl = pageUrl.ToLowerInvariant();

        if (activeRoute is null)
        {
            return "";
        }

        switch (pageUrl)
        {
            case "/index" when activeRoute == "/index":
                return activeRoute == pageUrl ? "active" : activeRoute;
            case "/index":
                return "";
        }

        pageUrl = pageUrl.Replace("/index", "");
        var highLevelActiveRoute = GetHighLevelRoute(activeRoute); // "/organizations/ng-coders" => "organizations"

        return highLevelActiveRoute.Contains(pageUrl) ? "active" : highLevelActiveRoute;
    }

    private string GetHighLevelRoute(string route)
    {
        route.EnsureStartsWith('/');
        return route.Split("/")[1].EnsureStartsWith('/').EnsureEndsWith('/');
    }

}
